package com.ice.delay.task.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ice.delay.task.core.constant.ServerErrorCode;
import com.ice.delay.task.core.model.DelayTaskUser;
import com.ice.delay.task.dao.DelayTaskUserDao;
import com.ice.delay.task.service.DelayTaskUserService;
import com.ice.delay.task.vo.req.LoginVoReq;
import com.ice.delay.task.vo.req.UserCreateVoReq;
import com.ice.delay.task.vo.req.UserDeleteVoReq;
import com.ice.delay.task.vo.req.UserVoReq;
import com.ice.delay.task.vo.res.UserVoRes;
import com.ice.framework.common.base.RPage;
import com.ice.framework.common.exception.BusinessException;
import com.ice.framework.mybatisplus.util.MyBatisUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangwei
 * @Date 2023/5/12 09:50
 */
@Service
public class DelayTaskUserServiceImpl extends ServiceImpl<DelayTaskUserDao, DelayTaskUser> implements DelayTaskUserService {

    @Override
    public DelayTaskUser login(LoginVoReq loginVoReq) {
        DelayTaskUser user = this.getBaseMapper().getByAccountCode(loginVoReq.getAccount());
        if (ObjectUtil.isEmpty(user)) {
            throw new BusinessException(ServerErrorCode.USER_NOT_FOUND);
        }
        if (!user.getPassword().equals(loginVoReq.getPassword())) {
            throw new BusinessException(ServerErrorCode.LOGIN_PASSWORD_NOT_CORRET);
        }
        return user;
    }

    @Override
    public RPage<UserVoRes> pageUser(RPage<UserVoReq> page) {
        IPage<DelayTaskUser> iPage = new Page<>(page.getCurrent(), page.getSize());
        IPage<DelayTaskUser> resultPage = this.getBaseMapper().selectPage(iPage, new LambdaQueryWrapper<>());
        return MyBatisUtils.transmit(resultPage, UserVoRes.class);
    }

    @Override
    public void createUser(UserCreateVoReq userCreateVoReq) {
        DelayTaskUser user = this.getBaseMapper().getByAccountCode(userCreateVoReq.getAccountCode());
        if (ObjectUtil.isNotEmpty(user)) {
            throw new BusinessException(ServerErrorCode.ACCOUNT_CODE_EXISTS);
        }
        DelayTaskUser delayTaskUser = new DelayTaskUser();
        delayTaskUser.setAccountCode(userCreateVoReq.getAccountCode());
        delayTaskUser.setName(userCreateVoReq.getName());
        delayTaskUser.setCreateTime(new Date());
        delayTaskUser.setPassword(DigestUtils.md5DigestAsHex(DelayTaskUser.DEFAULT_PWD.getBytes(StandardCharsets.UTF_8)));
        this.getBaseMapper().insert(delayTaskUser);
    }

    @Override
    public void deleteUser(UserDeleteVoReq userDeleteVoReq) {
        List<String> ids = Stream.of(userDeleteVoReq.getIds().split(",")).collect(Collectors.toList());
        List<DelayTaskUser> delayTaskUsers = this.getBaseMapper().selectList(new LambdaQueryWrapper<DelayTaskUser>().eq(DelayTaskUser::getId, ids));
        if (delayTaskUsers.isEmpty() || ids.size() != delayTaskUsers.size()) {
            throw new BusinessException(ServerErrorCode.USER_NOT_FOUND);
        }
        for (DelayTaskUser delayTaskUser : delayTaskUsers) {
            if (delayTaskUser.getAccountCode().equals("admin")) {
                throw new BusinessException(ServerErrorCode.SYSTEM_USER_NOT_DELETED);
            }
        }
        this.getBaseMapper().delete(new LambdaQueryWrapper<DelayTaskUser>().in(DelayTaskUser::getId, ids));
    }
}
